package com.blue.controller.system;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.annotation.ReplaceParam;
import com.blue.common.annotation.NotNull;
import com.blue.common.entity.ResponseResult;
import com.blue.common.utils.ConvertUtils;
import com.blue.common.utils.QueryWrapperUtils;
import com.blue.dto.system.DictItemDTO;
import com.blue.po.system.DictItemPO;
import com.blue.service.system.IDictItemService;
import com.github.yulichang.query.MPJQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 数据字典子表 前端控制器
 * </p>
 *
 * @author 陈南杰
 * @since 2023-09-22
 */
@RestController
@RequestMapping("/system/dictItem")
@Tag(name = "系统字典子集")
public class DictItemController {
    @Autowired
    private IDictItemService dictItemService;

    @GetMapping("/pageList")
    @Operation(summary = "通过ID查询字典子集")
    @ReplaceParam("code")
    public ResponseResult<?> pageList(DictItemDTO requestDTO,
                                      @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                      @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        DictItemPO dictItemPO = ConvertUtils.convert(DictItemPO.class, requestDTO);
        MPJQueryWrapper<DictItemPO> queryWrapper = QueryWrapperUtils.initQueryWrapper(dictItemPO);
        Page<DictItemPO> page = new Page<>(pageNum, pageSize);
        Page<DictItemPO> pageResult = dictItemService.page(page, queryWrapper);
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/add")
    @Operation(summary = "新增子字典")
    public ResponseResult<?> add(@RequestBody DictItemDTO requestDTO) {
        List<DictItemPO> dictItemPOList = dictItemService.list(new LambdaQueryWrapper<DictItemPO>()
                .eq(DictItemPO::getDictId, requestDTO.getDictId())
                .eq(DictItemPO::getCode, requestDTO.getCode()));
        if (CollectionUtil.isNotEmpty(dictItemPOList)) {
            return ResponseResult.errorResult("该编号字典已存在，请重新输入");
        }
        DictItemPO dictItemPO = ConvertUtils.convert(DictItemPO.class, requestDTO);
        dictItemService.save(dictItemPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除子字典")
    @NotNull(value = "ids", message = "字典ID不能为空！")
    public ResponseResult<?> delete(@RequestBody List<String> ids) {
        dictItemService.removeBatchByIds(ids);
        return ResponseResult.okResult();
    }

    @PostMapping("/edit")
    @Operation(summary = "修改子字典")
    @NotNull(value = "id", message = "用户ID不能为空！")
    public ResponseResult<?> edit(@RequestBody DictItemDTO requestDTO) {
        DictItemPO dictItemPO = ConvertUtils.convert(DictItemPO.class, requestDTO);
        dictItemService.updateById(dictItemPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/queryById")
    @Operation(summary = "通过ID查询")
    @NotNull(value = "id", message = "用户ID不能为空！")
    public ResponseResult<?> queryById(@RequestBody DictItemDTO requestDTO) {
        DictItemPO dictItemPO = dictItemService.getById(requestDTO.getId());
        return ResponseResult.okResult(dictItemPO);
    }
}
